package com.example.front.serivce;

import com.example.common.dto.ListDto;
import com.example.common.dto.Pagination;
import com.example.common.exception.BusinessException;
import com.example.front.dto.*;
import com.example.front.entity.Factory;
import com.example.front.entity.Gateway;
import com.example.front.entity.Workshop;
import com.example.front.mapper.WorkshopMapper;
import com.example.interceptor.UserContext;
import com.example.passport.entity.User;
import com.example.passport.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class WorkshopService {

    @Resource
    WorkshopMapper workshopMapper;

    @Resource
    UserService userService;

    @Resource
    LineService lineService;

    @Resource
    FactoryService factoryService;

    /**
     * 创建车间
     */
    public void createWorkshop(WorkshopCreateDto workshopCreateDto) {
        User me = UserContext.getUser();

        if (workshopMapper.findByName(me.getTenantId(), workshopCreateDto.getName()) != null) {
            throw new BusinessException("名称已存在");
        }
        userService.findNameById(workshopCreateDto.getUserId());
        Workshop workshop = new Workshop();
        BeanUtils.copyProperties(workshopCreateDto, workshop);
        workshop.setTenantId(me.getTenantId());
        workshopMapper.create(workshop);
    }

    /**
     * 列出车间
     */
    public ListDto<WorkshopListDto> listWorkshop(WorkshopSearchDto condition, Pagination pagination) {

        long total = workshopMapper.count(condition);
        if (total == 0) {
            return new ListDto<>(new ArrayList<>(), pagination);
        }

        pagination.setTotal(total);

        List<Workshop> workshopList = workshopMapper.findAll(condition, pagination);

        List<WorkshopListDto> workshopDtoList = workshopList.stream().map(workshop -> {
            WorkshopListDto workshopListDto = new WorkshopListDto();
            BeanUtils.copyProperties(workshop, workshopListDto);
            User user=userService.findUserById(workshop.getUserId());
            Factory factory=factoryService.findById(workshop.getFactoryId(), UserContext.getUser().getTenantId());
            if(user!=null){
                workshopListDto.setUserName(user.getName());
            }else {
                workshopListDto.setUserName("");
            }
            if(factory!=null){
                workshopListDto.setFactoryName(factory.getName());
            }else {
                workshopListDto.setFactoryName("");
            }
            return workshopListDto;
        }).collect(Collectors.toList());

        return new ListDto<>(workshopDtoList, pagination);
    }

    /**
     * 跟新数据
     */
    public void update(@Valid WorkshopDto workshopDto) {
        User me = UserContext.getUser();

        if (workshopMapper.findByName(me.getTenantId(), workshopDto.getName()) != null && workshopMapper.findByIdName(me.getTenantId(), workshopDto.getId(), workshopDto.getName()) == null) {
            throw new BusinessException("名称已存在");
        }
        userService.findUserById(workshopDto.getUserId());
        Workshop workshop = new Workshop();
        workshop.setTenantId(me.getTenantId());
        BeanUtils.copyProperties(workshopDto, workshop);
        if (workshopMapper.update(workshop) != 1) {
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 删除
     */
    public void deleteById(long id,long tenantId) {
        if(lineService.findByWorkshopId(id,tenantId)!=0){
            throw new BusinessException("该车间下存在有产线，请将该车间下的产线删除或修改再执行该操作");
        }
        if (workshopMapper.deleteById(id, tenantId) != 1) {
            throw new BusinessException("操作失败");
        }
    }

    /**
     * 查找车间
     */
    public Workshop findById(long id, long tenantId) {
        Workshop workshop = workshopMapper.findById(id, tenantId);
        if (workshop == null) {
            return Workshop.builder().name("该车间已被删除").build();
        }
        return workshop;
    }
    /**
     * 查找车间
     */
    public Workshop findById(long id) {
        Workshop workshop = workshopMapper.findByID(id);
        if (workshop == null) {
            return Workshop.builder().name("该车间已被删除").build();
        }
        return workshop;
    }
}
